<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Themes Library Class
 *
 * @package	cisvn
 * @subpackage	Libraries
 * @category	Template
 * @author		Nguyen Hai Dang
 * @copyright	Copyright (c) 2008 All rights reserved.
 */
 
class Theme_lib 
{	
//--------------------------------------------------------------------------------------------------
	var $settings;
	var $theme_data;
	var $folder_by_type = array('theme_frontend/', 'theme_backend/', 'theme_userpage/');
	
	function Theme_lib() {
		$this->CI =& get_instance();			
	}
//--------------------------------------------------------------------------------------------------	
	function render($layout, $data=null, $container='container', $header=null, $footer=null)	{

		$r_data['view'] = $this->theme_data['views_folder'].$layout;
		$r_data['data'] = $data;
		if ($header != null) {$this->CI->load->view($this->theme_data['views_folder'].$header);}		
		$this->CI->load->view($this->theme_data['views_folder'].$container, $r_data);
		if ($footer != null) {$this->CI->load->view($this->theme_data['views_folder'].$footer);}		
	}	
//--------------------------------------------------------------------------------------------------
	function load_theme_data($id_theme = 0, $theme_type = 0) {	
		$this->theme_data = $this->get_theme_from_id($id_theme, $theme_type);			
	}
	
	function get_theme_from_id($id_theme=0, $theme_type=0)
	{		
		if ($id_theme == 0) {
			$out = array(
				'name' => 'default',
				'folder' => $this->folder_by_type[$theme_type].'default/',		
				'assets_folder' => $this->folder_by_type[$theme_type].'default/',	
				'views_folder' => $this->folder_by_type[$theme_type].'default/',			
				'type' => $theme_type
			);
		}
		else {			
			$this->CI->db->select('*')->from('themes')->where('id', $id_theme);
			$temp = $this->CI->db->get()->row();
			$out = $this->get_theme_from_object($temp);
		}
		return $out;
	}
	
//--------------------------------------------------------------------------------------------------
	function get_theme_from_object($theme) {				
		$out = array(
			'name' => $theme->name,
			'assets_folder' => $this->folder_by_type[$theme->type].$theme->assets_folder.'/',
			'views_folder' => $this->folder_by_type[$theme->type].$theme->views_folder.'/',
			'type' => $theme->type
		);		    		
		return $out;
	}
//--------------------------------------------------------------------------------------------------
	function get_all_by_type($theme_type=0)
	{
		$this->CI->db->select('*')->from('themes')->where('type', $theme_type);
		return $this->CI->db->get()->result();
	}
	
	function update_by_id($theme_id, $theme_type) 
	{
		$this->theme_data = $this->get_theme_from_id($theme_id, $theme_type);
		$z = array('frontend_theme','backend_theme','userpage_theme');
		$userdata[$z[$theme_type]] = $this->theme_data;
		$this->CI->db_session->set_userdata($userdata);	    
	}
	
}

// EOF
?>